import PaginationDecorator from '../../core/controllers/Pagination.controller.decorator';

@Inject('$log', '$scope', '$http', 'documentService', 'ngDialogService', 'box', 'ngDialog')
class DocumentController extends PaginationDecorator() {
  constructor() {
    super(documentService);

    this.view = {
      box,
    };

    this.resource = {
      documentList: [],
    };

    this.$init();
  }

  $init() {
    this.mapParams.orgId = this.$scope.orgId;

    this.$setQueryCallback((res) => {
      this.resource.documentList = res.rows;
    });

    if (!this.box.children.length) {
      this.mapParams.type = this.box.id;
      this.$paginationQuery();
    } else {
      this.selectFolder(this.box.children[0]);
    }
  }

  getDownloadSrc(id) {
    return `/sgms/api/v1/common/downLoad/${id}`;
  }

  selectFolder(folder) {
    this.view.box.children.forEach((item) => {
      item.active = false;
    });
    folder.active = true;
    this.mapParams.type = folder.id;
    this.$paginationQuery();
  }

  getImgUrl() {
    const boxName = this.view.box.name;
    /*eslint-disable */
    return require(`../../assets/images/document/${boxName}.png`);
  }

  $addDocument(ctrl) {
    this.ngDialog.open({
      template: 'paAddDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($rootScope, $scope, ngDialogService) {
        'ngInject';

        this.view = {
          attachmentFiles: [],
        };

        this.reqData = {
          orgId: $rootScope.orgId || storageApi.get('orgId'),
          documentName: '',
          documentNumber: '',
          compileTime: '',
          remark: '',
          attachmentIds: [],
          type: ctrl.mapParams.type,
        };

        this.submitForm = () => {
          this.reqData.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          ctrl.documentService.save(this.reqData)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess(res.message);
                $scope.closeThisDialog();
                ctrl.$paginationQuery();
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $editDocument(params, ctrl) {
    this.ngDialog.open({
      template: 'paEditDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($scope, ngDialogService) {
        'ngInject';

        ctrl.documentService.get({ id: params.id })
          .then((res) => {
            if (res.success) {
              this.document = res.data;
              this.view = {
                attachmentFiles: this.document.files,
              };
              this.view.attachmentFiles.forEach((item) => {
                item.name = item.fileName;
              });
            } else {
              ngDialogService.alertError(res.message);
            }
          });

        this.submitEditForm = () => {
          this.document.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          ctrl.documentService.update(this.document)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess(res.message);
                $scope.closeThisDialog();
                ctrl.$paginationQuery();
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $delete(id) {
    this.ngDialogService.confirm('确定删除此数据吗？', id)
      .then((data) => {
        this.$log.debug(data);
        this.documentService
          .delete([id])
          .then((res) => {
            if (res.success) {
              this.ngDialogService.alertSuccess(res.message);
              this.$paginationQuery();
            }
          });
      });
  }
}

export default DocumentController;
